<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="门店名称11" prop="placePointName">
        <el-input
          v-model="queryParams.placePointName"
          placeholder="请输入内容"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择内容">
          <el-option :key="0" value="正常"></el-option>
          <el-option :key="1" value="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
         
          class="add"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button
          icon="el-icon-refresh"
          class="delBtn"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          class="add"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          class="delBtn"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getListData" />
    </el-row>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
      style="margin-top: 20px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门店名称" prop="placePointName" />
      <el-table-column label="M5门店ID" prop="m5Id" />
      <el-table-column label="员工数量" prop="description" />
      <el-table-column label="用户联系次数" prop="description" />
      <el-table-column label="用户领用次数" prop="description" />
      <el-table-column label="地址" prop="description" />
      <el-table-column label="联系电话" prop="description" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div class="statusBox">
            <div class="status_div wcl" v-if="scope.row.status == 0">正常</div>
            <div class="status_div jxz" v-if="scope.row.status == 1">禁用</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            class="btnText"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
    <el-dialog :title="title" :visible.sync="open" width="786px" append-to-body>
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addRules"
        label-position="top"
      >
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="addForm.phone" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="患者备注" prop="description">
              <el-input
                v-model="addForm.description"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="登记时间" prop="signTime">
              <el-date-picker
                v-model="addForm.signTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="请选择日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对接人" prop="contactByName">
              <el-input
                disabled
                v-model="user.nickName"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="imageUrls">
              <span slot="label"> 图片 </span>
              <el-upload
                class="upload-demo"
                :action="uploadUrl"
                :before-upload="handleBeforeUpload"
                :on-success="handleUploadSuccess"
                :on-remove="handleRemove"
                :headers="headers"
                :before-remove="beforeRemove"
                :limit="5"
                multiple
                :on-exceed="handleExceed"
                :file-list="fileList"
                list-type="picture"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="addForm.remark" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" class="add" @click="submitForm">{{
          addForm.id ? "确认编辑" : "确认添加"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addData, editData } from "@/api/medicineManage/index";
import { getToken } from "@/utils/auth";
import { getUserProfile } from "@/api/system/user";
export default {
  name: "ShopIndex",
  data() {
    return {
      uploadUrl:
        process.env.VUE_APP_BASE_API + "/common/uploadPc/goodsRegister", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      user: {},
      fileList: [],
      title: "添加",
      open: false,
      tableKey: 0,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 10,
      // 角色表格数据
      roleList: [],
      // 查询参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        placePointName: "",
        signTime: "",
      },
      addForm: {
        phone: "",
        signTime: "",
        description: "",
        imageUrls: [],
        remark: "",
        contactByName: "",
      },
      addRules: {
        phone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        signTime: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
      systemList: [],
    };
  },
  created() {
    this.getListData();
    getUserProfile().then((res) => {
      this.user = res.data;
    });
  },
  methods: {
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.infoId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.selectName = selection.map((item) => item.userName);
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
      const isJPG = type.includes(file.type);
      // 检验文件格式
      if (!isJPG) {
        this.$message.error(`图片格式错误!`);
        return false;
      }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 < 500;
        if (!isLt) {
          this.$message.error(`上传文件大小不能超过 500 KB!`);
          return false;
        }
      }
      return true;
    },
    handleRemove(file, fileList) {
      console.log(file);
      const list = this.addForm.imageUrls.filter(
        (item) => item !== file.response.fileName
      );
      this.addForm.imageUrls = list;
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleUploadSuccess(res, file) {
      // 如果上传成功
      if (res.code == 200) {
        this.addForm.imageUrls.push(res.fileName);
      } else {
        this.$message.error("图片上传失败");
      }
    },
    /** 查询列表 */
    getListData() {
      this.loading = true;
      getList({
        ...this.queryParams,
        startDate: this.queryParams.signTime[0],
        endDate: this.queryParams.signTime[1],
      }).then((response) => {
        this.roleList = response.data.records;
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$nextTick(() => {
        this.getListData();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNum = 1;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加按钮操作 */
    handleAdd() {
      this.title = "添加";
      this.open = true;
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "编辑";
      console.log(row);
      this.addForm.id = row.id;
      this.addForm.phone = row.phone;
      this.addForm.description = row.description;
      this.addForm.remark = row.remark;
      this.addForm.signTime = row.signTime;
      this.addForm.imageUrls = row.imageUrls;
      this.fileList = row.imageUrls.map((item) => {
        return {
          name: item.split("/").pop(),
          url: "/api" + item,
        };
      });
      this.open = true;
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.addForm = {
        phone: "",
        signTime: "",
        description: "",
        imageUrls: [],
        remark: "",
      };
      this.fileList = [];
      this.resetForm("addForm");
    },
    /** 按钮操作 */
    handleDelete(row) {
      actionData({ id: row.id, status: row.status == 0 ? 1 : 2 }).then(
        (response) => {
          this.$modal.msgSuccess("操作成功");
          this.getListData();
        }
      );
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          if (this.addForm.imageUrls.length == 0)
            return this.$message.error("请上传图片");
          if (this.addForm.id) {
            editData({
              ...this.addForm,
              contactByName: this.user.nickName,
            }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListData();
            });
          } else {
            addData({
              ...this.addForm,
              contactByName: this.user.nickName,
            }).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getListData();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.footerForm {
  margin-left: 200px;
}
.formBox {
  width: 600px;
  margin-top: 40px;
}
.btnText {
  color: #0052d9 !important;
}
.dialogForm {
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep.el-form--label-top .el-form-item__label {
    padding: 0;
  }
}
.add {
  background: #0052d9;
  color: #fff;
  border: none;
}
.delBtn {
  background: #e7e7e7;
  color: #000;
  border: none;
}
.open {
  width: 52px;
  height: 32px;
  background: #e3f9e9;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #2ba471;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #2ba471;
}
.close {
  width: 52px;
  height: 32px;
  background: #d8d8d8;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #767676;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #767676;
}
.statusBox {
  width: 100%;
  display: flex;
  justify-content: center;
}
.status_div {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 32px;
  background: #ffffff;
  border-radius: 3px;
  border: 1px solid #e7e7e7;
  font-size: 14px;
  background: #f3f3f3;
  color: rgba(0, 0, 0, 0.9);
  line-height: 1;
  &.jxz {
    background: #f2f3ff;
    border: 1px solid #0052d9;
    color: #0052d9;
  }
  &.ywc {
    background: #e3f9e9;
    border: 1px solid #2ba471;
    color: #2ba471;
  }
}
.img {
  width: 38px;
  height: 38px;
}
.img_box {
  display: flex;
  justify-content: center;
  gap: 10px;
}
</style>
